package org.jash.shop2212;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.HashMap;

public class RegisterActivity extends AppCompatActivity {
    private EditText username, password, confirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        confirm = findViewById(R.id.confirm);
        findViewById(R.id.register).setOnClickListener(v -> {
            String un = username.getText().toString();
            String pw = password.getText().toString();
            String con = confirm.getText().toString();
            if (!pw.equals(con)) {
                Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                return;
            }
            HttpUtils.doPost("http://10.161.9.80:7012/user/register", new HashMap<String, Object>() {{
                        // 写入参数
                put("username", un);
                put("password", pw);
                put("repassword", con);
                    }}, LoginEntity.class,
                    r -> {
                        // 正确的处理
                        if (r.getCode() == 200) {
                            setResult(RESULT_OK, new Intent().putExtra("username", un).putExtra("password", pw));
                            finish();
                        } else {
                            Toast.makeText(this, r.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }, t -> {
                        // 错误的处理
                        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
                    });
        });
    }
}